<template>
  <el-card>
    <h2>借书人管理</h2>
    <el-form :inline="true" @submit.prevent>
      <el-form-item label="姓名">
        <el-input v-model="search.name" placeholder="输入姓名" />
      </el-form-item>
      <el-form-item>
        <el-button @click="getList">查询</el-button>
        <el-button type="primary" @click="showAdd = true">新增</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="list" style="width: 100%; margin-top: 20px;">
      <el-table-column prop="borrowerId" label="ID" width="60" />
      <el-table-column prop="name" label="姓名" />
      <el-table-column prop="idCard" label="身份证号" />
      <el-table-column prop="phone" label="电话" />
      <el-table-column prop="address" label="地址" />
      <el-table-column prop="status" label="状态">
        <template #default="scope">
          <el-tag :type="scope.row.status === 1 ? 'success' : 'danger'">
            {{ scope.row.status === 1 ? '正常' : '冻结' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="180">
        <template #default="scope">
          <el-button size="small" @click="editRow(scope.row)">编辑</el-button>
          <el-button size="small" type="danger" @click="delRow(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      v-model:current-page="page"
      :page-size="size"
      :total="total"
      @current-change="getList"
      layout="prev, pager, next"
      style="margin-top: 20px; text-align: right;"
    />
    <el-dialog v-model="showAdd" title="新增借书人">
      <el-form :model="form">
        <el-form-item label="姓名"><el-input v-model="form.name" /></el-form-item>
        <el-form-item label="身份证号"><el-input v-model="form.idCard" /></el-form-item>
        <el-form-item label="电话"><el-input v-model="form.phone" /></el-form-item>
        <el-form-item label="地址"><el-input v-model="form.address" /></el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="showAdd = false">取消</el-button>
        <el-button type="primary" @click="addRow">确定</el-button>
      </template>
    </el-dialog>
    <el-dialog v-model="showEdit" title="编辑借书人">
      <el-form :model="form">
        <el-form-item label="姓名"><el-input v-model="form.name" /></el-form-item>
        <el-form-item label="身份证号"><el-input v-model="form.idCard" /></el-form-item>
        <el-form-item label="电话"><el-input v-model="form.phone" /></el-form-item>
        <el-form-item label="地址"><el-input v-model="form.address" /></el-form-item>
        <el-form-item label="状态">
          <el-select v-model="form.status">
            <el-option :value="1" label="正常" />
            <el-option :value="0" label="冻结" />
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="showEdit = false">取消</el-button>
        <el-button type="primary" @click="updateRow">保存</el-button>
      </template>
    </el-dialog>
  </el-card>
</template>
<script setup>
import { ref, reactive, onMounted } from 'vue'
import axios from 'axios'
import { ElMessage } from 'element-plus'

const list = ref([])
const page = ref(1)
const size = ref(10)
const total = ref(0)
const search = reactive({ name: '' })
const showAdd = ref(false)
const showEdit = ref(false)
const form = reactive({})

const getList = async () => {
  const res = await axios.get('/api/borrower/list', { params: { page: page.value, size: size.value, name: search.name } })
  list.value = res.data.records
  total.value = res.data.total
}
onMounted(getList)

const addRow = async () => {
  try {
    await axios.post('/api/borrower/add', form)
    ElMessage.success('新增成功')
    showAdd.value = false
    getList()
  } catch (e) {
    ElMessage.error(e.response?.data || '新增失败')
  }
}
const editRow = (row) => {
  Object.assign(form, row)
  showEdit.value = true
}
const updateRow = async () => {
  try {
    await axios.post('/api/borrower/update', form)
    ElMessage.success('修改成功')
    showEdit.value = false
    getList()
  } catch (e) {
    ElMessage.error(e.response?.data || '修改失败')
  }
}
const delRow = async (row) => {
  try {
    await axios.post('/api/borrower/delete', null, { params: { id: row.borrowerId } })
    ElMessage.success('删除成功')
    getList()
  } catch (e) {
    ElMessage.error(e.response?.data || '删除失败')
  }
}
</script> 